/**
 * @name        pluginsCommon.ts
 * @author      shenyc
 * @date        2020-05-06 - ?
 * @copyright   Copy right (c) shenyc (shenyczz@163.com).
 *              All rights reserved.
 *
 ******************************************************************************/
// {{@@@}}
"use strict";
// ----------------------------------------------------------------------------
// official
import alias from "@rollup/plugin-alias";
import babel from "@rollup/plugin-babel";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import nodeResolve from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";
import terser from "@rollup/plugin-terser";
import typescript from "@rollup/plugin-typescript";
// community
import copy from "rollup-plugin-copy";
import nodePolyfills from "rollup-plugin-polyfill-node";
// ----------------------------------------------------------------------------
/**
 * pluginsCommon
 * - commonjs()
 * - json()
 * - typescript()
 * - nodeResolve()
 * - nodePolyfills()
 */
export const pluginsCommon = [
    /**
     * A Rollup plugin to convert CommonJS modules to ES6,
     * so they can be included in a Rollup bundle
     */
    commonjs({
        include: "**/node_modules/**",
        extensions: [".js", ".cjs"],
    }),
    /**
     * A Rollup plugin which Converts .json files to ES6 modules.
     */
    json(),
    /**
     * A Rollup plugin for seamless integration between Rollup and Typescript.
     * 默认使用同路径下的 tsconfig.json
     */
    typescript({
        // jsx: "preserve",
        cacheDir: "cache",
        outputToFilesystem: true,
        // tsconfig: path.resolve(".", "tsconfig.json")
    }),
    /**
     * A Rollup plugin which locates modules using the Node resolution algorithm,
     * for using third party modules in node_modules
     */
    nodeResolve({
        exportConditions: ["node", "browser"],
        browser: true,
        mainFields: ["main", "module", "unpkg"],
        extensions: [".js", ".mjs", ".json", ".node", ".vue"],
        preferBuiltins: true,
    }),
    /**
     * A modern Node.js polyfill for your Rollup bundle.
     */
    nodePolyfills({
        // sourceMap: false,
    }),
];
// ----------------------------------------------------------------------------
export default pluginsCommon;
// ----------------------------------------------------------------------------
// {{@@@}}

// temp
const _plugins_all = [
    // ------------------------------------------------------------------------
    // Official Plugins
    // ------------------------------------------------------------------------
    /**
     * A Rollup plugin for defining aliases when bundling packages.
     */
    alias({
        entries: [
            // 将 Vue 的构建版本切换为完整版，以支持运行时模板编译
            { find: "vue", replacement: "vue/dist/vue.esm-bundler.js" },
            // { find: "utils", replacement: "../../../utils" },
        ],
    }),
    /**
     * A Rollup plugin for seamless integration between Rollup and Babel.
     */
    babel({
        babelHelpers: "bundled", // 使用 bundled 模式
        exclude: ["**/node_modules/**"],
        extensions: [".js", ".jsx", ".es6", ".es", ".mjs", "tsx"], // 处理的文件扩展名
        presets: [
            "@babel/preset-env", // 使用 @babel/preset-env 进行转换
            "@babel/preset-typescript", // 处理 TypeScript
            "@vue/babel-preset-jsx", // uve 处理 jsx
        ],
    }),

    /**
     * A Rollup plugin which Converts .json files to ES6 modules.
     */
    json(),
    /**
     * A Rollup plugin to convert CommonJS modules to ES6,
     * so they can be included in a Rollup bundle
     */
    commonjs({ include: "node_modules/**" }),
    /**
     * A Rollup plugin which locates modules using the Node resolution algorithm,
     * for using third party modules in node_modules
     */
    nodeResolve({
        exportConditions: ["node", "browser"],
        browser: true,
        mainFields: ["main", "module", "unpkg"],
        extensions: [".mjs", ".js", ".json", ".node"],
        preferBuiltins: true,
    }),
    /**
     *  A Rollup plugin which replaces targeted strings in files while bundling.
     */
    replace({
        preventAssignment: true,
        __buildDate__: () => new Date().toString(), // e.g. toLocaleDateString("af" | "en" | "zh-cn")
    }),
    /**
     * A Rollup plugin to generate a minified bundle with terser.
     */
    terser({
        format: {
            comments: false,
        },
    }),
    /**
     * A Rollup plugin for seamless integration between Rollup and Typescript.
     * 默认使用同路径下的 tsconfig.json
     */
    typescript({
        cacheDir: "cache",
        outputToFilesystem: true,
        // tsconfig: path.resolve(".", "tsconfig.json")
    }),
    // ------------------------------------------------------------------------
    // Community Plugins
    // ------------------------------------------------------------------------
    /**
     * Copy files and folders, with glob support.
     */
    copy({
        targets: [
            { src: ["package.json"], dest: "dist" },
            { src: ["src/types/**/*.d.ts"], dest: "dist/types" },
            { src: ["dist"], dest: "example", rename: "myLibs" },
            { src: ["public"], dest: "example", rename: "assets" },
            { src: ["thirdParty"], dest: "example", rename: "libs" },
        ],
        verbose: true, // 显示复制的文件信息到输出窗口
    }),
    /**
     * A modern Node.js polyfill for your Rollup bundle.
     * 以下模块包括ES6特定版本，允许您在默认导入之外进行命名导入，如果您只使用此插件，则应该可以正常工作。
     * [  ]	path, events, querystring, punycode, constants, tty, domain
     * [* ]	assert*, process*, stream*, util*, buffer*, url*, string_decoder*, os*, timers*, zlib*
     * [*†] http*†, https*†
     * [*‡] console*‡,
     * [*§] vm*§
     * [∆ ] dns∆, dgram∆, child_process∆, cluster∆, module∆, net∆, readline∆, repl∆, tls∆
     * [˚ ] fs˚, crypto˚, perf_hooks˚
     *
     * † http 和 https模块实际上是相同的，不会根据协议进行区分
     * ‡ default export only, because it's console, seriously just use the global
     * § vm does not have all corner cases and has less of them in a web worker
     * ∆ not shimmed, just returns mock (不进行平移，只是返回一个模拟对象)
     * ˚ shimmed, but too complex to polyfill fully. Avoid if at all possible. Some bugs and partial support expected.
     *   (虽然可以使用，但过于复杂，无法完全多态化。如果可能的话，请避免使用。可能会出现一些错误和部分支持。)
     *
     * 并非所有包含的模块都能平滑地合并，
     * - stream（以及由此延伸出的所有需要它的东西，如http)是一团循环引用，几乎无法通过树状提取法消除。
     * - url 方法实际上只是指向url对象的快捷方式，因此这些方法也不容易通过树状提取法消除。
     * - punycode, path, querystring, events, util 和 process 则可以很好地进行树状提取法消除，特别是当你使用命名导入时。
     */
    nodePolyfills(),

    // rollup-plugin-vue
];
